/*
 * @lc app=leetcode.cn id=94 lang=typescript
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */


//  思路：
//  如果root不存在就return
//  分别进行 递归左子树、push根节点值、递归右子树
//  在外侧保存解集，内层定义函数进行递归

//  复杂度：O(n) O(n)

function inorderTraversal(root: TreeNode | null): number[] {
    const res: number[] = []
    const inorder = (root: TreeNode | null) => {
        if (!root) return
        if (root.left) inorder(root.left)
        res.push(root.val)
        if (root.right) inorder(root.right)
    }
    inorder(root)
    return res
};
// @lc code=end
import { TreeNode } from './type'
const root = new TreeNode(1, null, new TreeNode(2, new TreeNode(3), null))

console.log(inorderTraversal(root));


